package com.itheima.zhaohang.mymoreitemsclicke;

import android.content.Context;
import android.os.Build;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.support.annotation.Nullable;
import android.support.annotation.RequiresApi;
import android.util.AttributeSet;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

/**
 * Created by 赵航 on 2017/7/22.
 */

public class MyToobar extends RelativeLayout {

    private  ImageView mIageView;
    private TextView mTextView;
    private String text=null;
    private Handler mHandler = new Handler(Looper.getMainLooper()){
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            if (text!=null){
                mTextView.setText(text);
            }

        }
    };


    public MyToobar(Context context) {
        this(context,null);
    }

    public MyToobar(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        View inflate = inflate(getContext(), R.layout.actionbar, this);
        mIageView = (ImageView) inflate.findViewById(R.id.toobarimageview);
        mTextView = (TextView) inflate.findViewById(R.id.toobartext_use);

        initData();

    }

    private void initData() {
        mIageView.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {
                if (mOnClickeListener!=null){
                    mOnClickeListener.onImageViewClickeListener();
                }

            }
        });
        mTextView.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {
                if (mOnClickeListener!=null){
                    mOnClickeListener.onTextViewClickeListener();
                }
            }
        });

    }

    public MyToobar(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        this(context, attrs);
    }

    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    public MyToobar(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        this(context, attrs);
    }

    /**
     * 设置中间文字的
     * @param mane
     */
    public void getText(String mane) {
        text = mane;
        mHandler.sendEmptyMessage(0);
    }


    /**
     * 标准的自定义接口
     */
    interface OnClickeListener{
        void onImageViewClickeListener();

        void onTextViewClickeListener();

    }

    private OnClickeListener mOnClickeListener=null;

    public void setOnIconClickeListener(OnClickeListener onClickeListener){
        mOnClickeListener = onClickeListener;
    }
    //向外暴露一个端口
    public TextView getChildTextView(){

        return mTextView;
    }
}
